/**
 * Created by Administrator on 2017/3/5 0005.
 */
var LightLineShader={
    create:function (texture,_w,_h) {
        var lightLine={};
        lightLine.kaleShader={
            uniforms : {
                time:       { value: 1.0 },
                speed:        {value:10.0},
                fadeAway:        {value:0.5},
                alpha:         {value:0.5},
                color:        {value:new THREE.Color(0x111111)},
                //color:        {value:new THREE.Color(0.79,0.51,0.2)},
                mouse:        {value:new THREE.Vector2(0.5,0.5)},
                resolution: { value: new THREE.Vector2(0.5,0.5) },
                uniformity:{value:10.0}
            },

            fragmentShader: [
                "precision highp float;",

                "varying vec2 vUv;",
                "uniform float time;",
                "uniform float speed;",
                "uniform float alpha;",
                "uniform float fadeAway;",
                "uniform vec3 color;",
                "uniform vec2 mouse;",
                "uniform vec2 resolution;",
                "uniform float uniformity;",

                "void main(void) {",
                "float t = time * speed;",
                "vec2 position = (vUv.xy - resolution.xy * .5) / resolution.x;",
                "float angle = atan(position.y, position.x) / (2. * 3.14159265359);",
                "angle -= floor(angle);",
                "float rad = length(position);",
                "float angleFract = fract(angle * 256.);",
                "float angleRnd = floor(angle * 256.) + 1.;",
                "float angleRnd1 = fract(angleRnd * fract(angleRnd * .7235) * 45.1);",
                "float angleRnd2 = fract(angleRnd * fract(angleRnd * .82657) * 13.724);",
                "float t2 = t + angleRnd1 * uniformity;",
                "float radDist = sqrt(angleRnd2);",
                "float adist = radDist / rad * .1;",
                "float dist = (t2 * .1 + adist);",
                "dist = abs(fract(dist) - fadeAway);",

                "float outputColor = (1.0 / (dist)) * cos(0.7 * sin(t)) * adist / radDist / 30.0;",
                "angle = fract(angle + .61);",
                " gl_FragColor = vec4(outputColor * color, alpha);",

                "}"
            ].join( '\n' ),

            vertexShader: [
                /**
                 * Example Vertex Shader
                 * Sets the position of the vertex by setting gl_Position
                 */

                "precision highp float;",
                "precision highp int;",


                "uniform float time;",

                "attribute vec2 uv2;",

                "varying vec3 vPosition;",
                "varying vec3 vNormal;",
                "varying vec2 vUv;",
                "varying vec2 vUv2;",

                "void main() {",

                // To pass variables to the fragment shader, you assign them here in the
                // main function. Traditionally you name the varying with vAttributeName
                "vNormal = normal;",
                "vUv = uv;",
                "vUv2 = uv2;",
                "vPosition = position;",

                // This sets the position of the vertex in 3d space. The correct math is
                // provided below to take into account camera and object data.
                "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

                "}"
            ].join( '\n' )
        }


        // kale.init=function(_w,_h)
        // {
        var geometry = new THREE.PlaneBufferGeometry( 500, 500 );
        var material = new THREE.ShaderMaterial( {
            uniforms: lightLine.kaleShader.uniforms,
            vertexShader: lightLine.kaleShader.vertexShader,
            fragmentShader: lightLine.kaleShader.fragmentShader,
            // side:THREE.DoubleSide,
            transparent:true
        } );
        // console.log(material.uniforms.iChannel0);
        // material.uniforms.iChannel0.value.wrapS = material.uniforms.iChannel0.value.wrapT = THREE.RepeatWrapping;
        lightLine.mesh = new THREE.Mesh( geometry, material );
        // }

        return lightLine;
    }
}